Skip to main content

Section II Q-1: Short Questions (Any Five) (5 Marks)

Questions

i) What is a Canvas in Android?
ii) What are Application Preferences in Android?
iii) What is the Telephony API in Android?
iv) How can you work with Shapes in Android?
v) What is SQLite Database in Android?
vi) How can you use Android Google Maps in your application?
vii) What are the key features of the Telephony API?

Answers

i) What is a Canvas in Android?

Canvas is a 2D drawing surface in Android that provides methods for drawing onto a bitmap or view.

Key Features:

  • Drawing Surface: Provides a 2D surface for custom drawing
  • Graphics Operations: Supports drawing shapes, text, bitmaps, and paths
  • Paint Integration: Works with Paint objects for styling
  • Hardware Acceleration: Can be hardware accelerated for better performance

Common Methods:

// Drawing shapes
canvas.drawRect(left, top, right, bottom, paint);
canvas.drawCircle(cx, cy, radius, paint);
canvas.drawLine(startX, startY, stopX, stopY, paint);

// Drawing text
canvas.drawText("Hello", x, y, paint);

// Drawing bitmaps
canvas.drawBitmap(bitmap, x, y, paint);

ii) What are Application Preferences in Android?

Application Preferences are key-value pairs used to store small amounts of data persistently in Android applications.

Types of Preferences:

  1. Shared Preferences: Store private primitive data in key-value pairs
  2. Preference Fragments: UI components for settings screens

SharedPreferences Example:

// Storing data
SharedPreferences sharedPref = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("username", "john_doe");
editor.putInt("user_age", 25);
editor.apply();

// Retrieving data
String username = sharedPref.getString("username", "default_user");
int age = sharedPref.getInt("user_age", 0);

Use Cases:

  • User settings and configurations
  • Login credentials
  • Application state
  • User preferences

iii) What is the Telephony API in Android?

Telephony API provides access to telephony-related information and functionality on Android devices.

Key Components:

  • TelephonyManager: Main class for telephony operations
  • Phone State Listener: Monitor phone state changes
  • SMS Manager: Send and receive SMS messages
  • Call Log: Access call history

Common Operations:

TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

// Get device information
String deviceId = telephonyManager.getDeviceId();
String networkOperator = telephonyManager.getNetworkOperatorName();
int phoneType = telephonyManager.getPhoneType();

Required Permissions:

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.SEND_SMS" />

iv) How can you work with Shapes in Android?

Shapes in Android can be created using XML drawable resources or programmatically using Canvas and Paint.

XML Drawable Shapes:

<!-- Rectangle Shape -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#FF5722" />
<corners android:radius="8dp" />
<stroke android:width="2dp" android:color="#000000" />
</shape>

<!-- Circle Shape -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#2196F3" />
<size android:width="100dp" android:height="100dp" />
</shape>

Programmatic Shapes:

// Using Canvas
Paint paint = new Paint();
paint.setColor(Color.BLUE);
paint.setStyle(Paint.Style.FILL);

// Draw rectangle
canvas.drawRect(100, 100, 300, 200, paint);

// Draw circle
canvas.drawCircle(200, 300, 50, paint);

// Draw custom path
Path path = new Path();
path.moveTo(100, 400);
path.lineTo(200, 350);
path.lineTo(300, 400);
path.close();
canvas.drawPath(path, paint);

v) What is SQLite Database in Android?

SQLite Database is a lightweight, embedded relational database management system used for local data storage in Android applications.

Key Features:

  • Lightweight: Small footprint, no separate server process
  • ACID Compliant: Supports transactions
  • Cross-platform: Works on all Android devices
  • SQL Support: Standard SQL syntax

Basic Operations:

// Create/Open database
SQLiteDatabase db = openOrCreateDatabase("MyDB", MODE_PRIVATE, null);

// Create table
db.execSQL("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT, email TEXT)");

// Insert data
ContentValues values = new ContentValues();
values.put("name", "John Doe");
values.put("email", "john@example.com");
db.insert("users", null, values);

// Query data
Cursor cursor = db.rawQuery("SELECT * FROM users", null);
while (cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndex("name"));
String email = cursor.getString(cursor.getColumnIndex("email"));
}
cursor.close();

vi) How can you use Android Google Maps in your application?

Google Maps Integration allows you to embed interactive maps in your Android application.

Setup Steps:

  1. Get API Key: Obtain from Google Cloud Console
  2. Add Dependencies: Include Google Play Services
  3. Add Permissions: Location and internet permissions
  4. Implement MapFragment: Add map to your layout

Implementation Example:

// build.gradle (Module: app)
implementation 'com.google.android.gms:play-services-maps:18.1.0'

// AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="YOUR_API_KEY" />

// MainActivity.java
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {
private GoogleMap mMap;

@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;

// Add marker
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}

vii) What are the key features of the Telephony API?

Key Features of Telephony API:

  1. Phone State Monitoring

    • Call state changes (idle, ringing, off-hook)
    • Network state monitoring
    • Signal strength detection
  2. Device Information

    • Device ID and IMEI
    • Network operator details
    • Phone type (GSM, CDMA)
    • SIM card information
  3. SMS Functionality

    • Send and receive SMS messages
    • Access SMS database
    • Monitor SMS delivery status
  4. Call Management

    • Initiate phone calls
    • Access call log
    • Monitor call duration
  5. Network Information

    • Network type (2G, 3G, 4G, 5G)
    • Roaming status
    • Network availability

Example Implementation:

public class PhoneStateListener extends android.telephony.PhoneStateListener {
@Override
public void onCallStateChanged(int state, String phoneNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
// Phone is idle
break;
case TelephonyManager.CALL_STATE_RINGING:
// Phone is ringing
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
// Call is active
break;
}
}

@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
// Signal strength changed
int level = signalStrength.getLevel();
}
}

Summary Table

FeaturePurposeKey Classes
Canvas2D DrawingCanvas, Paint
PreferencesData StorageSharedPreferences
Telephony APIPhone FunctionsTelephonyManager
ShapesUI GraphicsDrawable, Canvas
SQLiteLocal DatabaseSQLiteDatabase
Google MapsMap IntegrationGoogleMap, MapFragment

← Back to RETEST 2024